<?php

include (realpath(dirname(__FILE__) . '/../oss_sdk.class.php'));

class Demo
{
    private $oss = null;

    public function __construct()
    {
        // 这里的'Your accessID'和'Your accessKey'请替换成你自己的用户ID和用户密钥
        $this->oss = new OSS_SDK('Your accessID', 'Your accessKey');

        $configs = array
        (
            'logDir'   => dirname(__FILE__),
            'timeout'  => 86400
        );
        $this->oss->setConfig($configs);
        $this->oss->setConfig('debug', false);
    }

    public function run()
    {
        // 自定义OSS相关对象名称
        $bucket = 'DemoBucket';
        $object = 'DemoObject.gif';
        $group = 'DemoGroup';
        $myfile = 'Demo.gif';
        $multi = 'DemoMulti.gif';

        // Bucket相关操作
        $responses[] = $this->oss->listBuckets();
        $responses[] = $this->oss->createBucket($bucket);
        $responses[] = $this->oss->deleteBucket('AnotherBucket');
        $responses[] = $this->oss->setBucketAcl($bucket, 'public-read');
        $responses[] = $this->oss->getBucketAcl($bucket);
        $responses[] = $this->oss->listObjects($bucket);
        $responses[] = $this->oss->deleteObjects($bucket, array('Demo.php'));
        $responses[] = $this->oss->listMultis($bucket);

        // Object相关操作
        $responses[] = $this->oss->mkdir($bucket, 'Demo');
        $responses[] = $this->oss->createObjectByContent($bucket, $object, file_get_contents($myfile));
        $responses[] = $this->oss->createObjectByFile($bucket, 'DemoUpload.gif', $myfile);
        $responses[] = $this->oss->getObject($bucket, $object, 'E:\DemoDownload.gif');
        $responses[] = $this->oss->copyObject($bucket, $object, $bucket, 'DemoCopy.gif');
        $responses[] = $this->oss->getObjectMeta($bucket, $object);
        $responses[] = $this->oss->deleteObject($bucket, 'DemoCopy.gif');
        $responses[] = $this->oss->isObjectExist($bucket, $object);
        $responses[] = $this->oss->objectURL($bucket, $object);

        // Multipart Upload
        $responses[] = $this->oss->multiInitiate($bucket, $multi);
        $responses[] = $this->oss->multiUploadPart($bucket, $multi, 'xxx', 1, array('stream' => $myfile));
        $responses[] = $this->oss->multiComplete($bucket, $multi, 'xxx', array(array("PartNumber"=>1,"ETag" => 'xxx')));
        $responses[] = $this->oss->multiAbort($bucket, $multi, 'xxx');
        $responses[] = $this->oss->multiListParts($bucket, $multi, 'xxx');
        $responses[] = $this->oss->multiLazyUpload($bucket, 'DemoMultiLazy.gif', array('stream' => $myfile));
        $responses[] = $this->oss->multiLazyUploadByDir($bucket, dirname(__FILE__));

        // Group相关操作
        $responses[] = $this->oss->createObjectGroup($bucket, $group, array($object));
        $responses[] = $this->oss->getObjectGroup($bucket, $group);
        $responses[] = $this->oss->getObjectGroupIndex($bucket, $group);
        $responses[] = $this->oss->getObjectGroupMeta($bucket, $group);
        $responses[] = $this->oss->deleteObjectGroup($bucket, $group);

        foreach ($responses as $response)
        {
            if ($response instanceof OSS_Response)
            {
                echo '|----------------------Start----------------------------------------------' . "\n";
                echo '|-Status:' . $response->status . "\n";
                echo '|-Body:' . "\n";
                echo $response->body . "\n";
                echo "|-Header:\n";
                echo print_r($response->header, true);
                echo '-----------------------End------------------------------------------------' . "\n\n";
            }
            else
            {
                var_dump($response);
            }
        }
    }
}

$demo = new Demo();
$demo->run();

?>